# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4

##
## Copyright (C) 2009 - George Y. Kussumoto <georgeyk.dev@gmail.com>
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU Lesser General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., or visit: http://www.gnu.org/.
##
##
""" Create Hero Page Definition """

import google.appengine.ext.db as db

from twitheroapp.base import BaseTemplatePage
from twitheroapp.models.hero import Hero
from twitheroapp.twitterapi import create_hero


class CreateHero(BaseTemplatePage):
    #FIXME: rename to createhero.html
    template_file = 'createhero.html'
    page_values = {}

    def get(self):
        self.template_render()

    def post(self):
        name = self.request.get("name")
        if not name:
            self.redirect('/')

        query = db.Query(Hero)
        name = name.lower()
        query.filter('name =', name)
        results = query.fetch(limit=1)
        if not results:
            try:
                hero = create_hero(name)
            #FIXME: we should find out what exception raise here.
            except:
                self.redirect('/')

        self.redirect('hero/%s' % name)
